<template>
  <el-container class="adminEdit back-white" ref="box">
    <el-main>
      <div class="mb075">
        <el-breadcrumb separator="/" class="dib vam mr100">
          <el-breadcrumb-item> 成员列表</el-breadcrumb-item>
          <el-breadcrumb-item>成员管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button type="text" class="dib vam" icon="el-icon-arrow-left" @click="$router.go(-1)">返回</el-button>
      </div>
      <div class="x-item x-item-none">
        <h5 class="mb075 x-border-bottom pdb075">
          <i class="el-icon-edit-outline mr05 dib vam"></i>
          <span class="x-fs-080 dib vam">账号管理</span>
        </h5>
        <el-form label-width="100px">
          <el-form-item label="管理员账号"><el-input v-model="account"></el-input></el-form-item>
          <el-form-item label="管理员姓名"><el-input v-model="name"></el-input></el-form-item>
          <el-form-item label="邮箱地址"><el-input v-model="email"></el-input></el-form-item>
          <el-form-item label="设置权限">
            <el-checkbox v-for="(auth,index) in authorityList" :key="auth" v-model="auth.check" name="type">{{auth.text}}</el-checkbox>
          </el-form-item>
          <el-form-item label="登录密码" v-if="!adminId"><el-input type="password" v-model="password"></el-input></el-form-item>
          <el-form-item>
            <el-button type="primary" v-if="!adminId" @click="onSubmit">添加</el-button>
            <el-button type="primary" size="medium" v-if="adminId" @click="onSubmit">编辑</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-main>
  </el-container>
</template>
<script type="text/ecmascript-6">
  import {createData,getDataDetail} from '../../api/api';
  let loading;
  export default{
    data() {
      return {
        adminId: this.$route.query.id, //要编辑的账号id
        account: '', //账号
        name: '',  //姓名
        email: '', //邮箱地址
        authorityList: [//权限列表 
          {text: '医生',val:2,check:false}, 
          {text: '处方单列表',val:3,check:false},
          {text: '处方单审核',val:3.1,check:false},
          {text: '处方单发货',val:3.2,check:false},
          {text: '药材管理',val:4,check:false},
          {text: '问诊管理',val:5,check:false},
          {text: '用户',val:6,check:false},
          {text: '内容',val:7,check:false},
          {text: '账号管理',val:8,check:false}
        ],
        authApply: [], //提交的权限列表数据
        password: '' //登录密码
      }
    },
    created(){
      if(this.adminId){ //如果是编辑 则初始化数据
        let o = {
          "model": "AdminUsers",
          "with":{
          },
          "condition": {
            "id": this.adminId
          }
        };
        getDataDetail(o).then((data) => {
          let result = data.data.data;
          this.account = result.admin_account;
          this.name = result.admin_name;
          this.email = result.email;
          let authArr = result.models.split(',');
          for(let i = 0; i < authArr.length; i++){
            for(let j = 0; j < this.authorityList.length; j++){
              if(authArr[i] == this.authorityList[j].val){
                this.authorityList[j].check = true;
              }
            }
          }
        })
      }
    },
    methods:{
      checkData(){
        if(!this.account){
          this.tip('请输入账号');
          return false;
        }
        if(!this.name){
          this.tip('请输入姓名');
          return false;
        }
        if(!this.email){
          this.tip('请输入邮箱');
          return false;
        }else if(!/^\w+@\w+\.\w+$/.test(this.email)){
          this.tip('请输入正确的邮箱');
          return false;
        }
        this.authApply = [];
        for(let i = 0; i < this.authorityList.length; i++){
          if(this.authorityList[i].check){
            this.authApply.push(this.authorityList[i].val);
          } 
        }
        if(!this.authApply.length){
          this.tip('请至少选择一个权限列表');
          return false;
        }
        if(!this.adminId && !this.password){
          this.tip('请输入登录密码');
          return false;
        }
        return true;
      },
      onSubmit(){
        if(!this.checkData()){
          return;
        }
        loading = this.$loading({
          target: '.adminEdit',
          text: '提交中,请稍后'
        });
        this.authApply.push(1);
        let body = {
            "model": "AdminUsers",
            "data": {
              "admin_account": this.account,
              "admin_name":this.name,
              "email":this.email,
              "models":this.authApply.join(',')
          }
        };
        if(!this.adminId){
          body.data.admin_password = this.password;
        }else{
          body.update_condition = {
            "id": this.adminId
          }
        }
        createData(body).then((data) => {
          loading.close();
          if(data.data.code == 200){
            this.tip('操作成功','success');
            setTimeout(() => {
              this.$router.go(-1);
            },1000)
          }else{
            this.tip('操作失败');
          }
        })
      },
      tip(msg,type){
        this.$message({type:type ? type :  'error',message: msg});
      }
    }
  }
</script>
<style>
  .adminEdit .el-input{
    width: 260px;
  }
</style>
